package com.coocaa.ops.admin.v1.module.provider.impl;

import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.dmp.biz.impl.DmpRegistryRlsInfoBizImpl;
import com.coocaa.ops.admin.v1.module.convert.ModuleSaveConverter;
import com.coocaa.ops.admin.v1.module.model.internal.ModuleBatchSaveModel;
import com.coocaa.ops.admin.v1.module.provider.IModuleInfoRlsUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleTabContentConfigInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description 模块信息原始表 关联信息更新提供者接口实现
 * @date : 2020-06-15 16:34:46
 */
@Service
public class ModuleInfoRlsUpdateProviderImpl implements IModuleInfoRlsUpdateProvider{
    @Autowired
    private DmpRegistryRlsInfoBizImpl dmpRegistryRlsInfoBiz;


    @Override
    public void saveRls(ModuleInfoEntity inputEntity, Integer userId) {
        ModuleBatchSaveModel saveModel = new ModuleBatchSaveModel();
         //上部版面dmp
        List<ModuleTabContentConfigInfo> topTabDmpList = inputEntity.getTopTabDmpList();
        handleDmpTab(inputEntity, topTabDmpList, 1, saveModel);
        List<ModuleTabContentConfigInfo> bottomTabDmpList = inputEntity.getBottomTabDmpList();
        handleDmpTab(inputEntity, bottomTabDmpList, 2, saveModel);
        dmpRegistryRlsInfoBiz.saveBatchAvoidEmpty(saveModel.getFinalDmpList());
    }

    @Override
    public void deleteAllRls(Integer id, Integer version) {
          dmpRegistryRlsInfoBiz.deleteDmpRls(id, ResourceTypeEnum.MODULE.getValue(), version);
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private void handleDmpTab(ModuleInfoEntity inputEntity,List<ModuleTabContentConfigInfo> tabDmpList, int seq, ModuleBatchSaveModel saveModel){
        if (CollectionUtils.isEmpty(tabDmpList)){
            return;
        }
        int contentSeq = 1;
        for (ModuleTabContentConfigInfo moduleTabContentConfigInfo : tabDmpList){
             if (InputParamsValidator.isDmpContentAvailable(moduleTabContentConfigInfo.getId(), moduleTabContentConfigInfo.getDmpRegistryInfo())){
                 DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = ModuleSaveConverter.getDmpRegistryRlsInfoEntity(inputEntity, moduleTabContentConfigInfo);
                 dmpRegistryRlsInfoEntity.setSubContainerSeq(seq);
                 dmpRegistryRlsInfoEntity.setSubContainerContentSeq(contentSeq);
                 saveModel.getFinalDmpList().add(dmpRegistryRlsInfoEntity);
                 contentSeq++;
             }
        }
    }
}
